package com.ruoyi.backend2.service.impl;

import com.ruoyi.backend2.domain.User;
import com.ruoyi.backend2.utils.jwt.JWTUtils;
import com.ruoyi.backend2.mapper.UserMapper;
import com.ruoyi.backend2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public String getUser(String username, String password) {
        User user = userMapper.getUser(username);
        if (user != null && user.getPassword().equals(password)) {
            // 返回用户信息
            user.setPassword(null);
            String token = JWTUtils.createToken(user);
            redisTemplate.opsForValue().set("user:" + user.getId(), user, 60 * 60, TimeUnit.SECONDS);
            return token;
        }
        return "";
    }
}
